/*
 * @(#)TrayPopup.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.awt.tray;

import org.eoti.awt.GraphicsUtil;

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.StringWriter;
import java.io.PrintWriter;

public class TrayPopup
	extends PopupMenu
	implements ActionListener, MouseListener, MouseMotionListener
{
	protected SystemTray systemTray;
	protected TrayIcon trayIcon;

	public TrayPopup(String toolTip)
			throws AWTException
	{
		super();

		if(!SystemTray.isSupported())
			throw new UnsupportedOperationException("SystemTray is not supported on this platform.");

		systemTray = SystemTray.getSystemTray();
		BufferedImage trayImage = GraphicsUtil.createImage(systemTray.getTrayIconSize());
		trayIcon = new TrayIcon(trayImage, toolTip, this);
		trayIcon.setImageAutoSize(true);
		trayIcon.addActionListener(this);
		trayIcon.addMouseListener(this);
		trayIcon.addMouseMotionListener(this);
		systemTray.add(trayIcon);
	}

	public SystemTray getSystemTray(){return systemTray;}
	public TrayIcon getTrayIcon(){return trayIcon;}

	public void exception(Exception e)
	{
		StringWriter text = new StringWriter();
		e.printStackTrace(new PrintWriter(text));
		error(e.getMessage(), text.getBuffer().toString());
	}

	public void error(String caption, String text)
	{
		trayIcon.displayMessage(caption, text, TrayIcon.MessageType.ERROR);
	}

	public void info(String caption, String text)
	{
		trayIcon.displayMessage(caption, text, TrayIcon.MessageType.INFO);
	}

	public void warning(String caption, String text)
	{
		trayIcon.displayMessage(caption, text, TrayIcon.MessageType.WARNING);
	}

	public void message(String caption, String text)
	{
		trayIcon.displayMessage(caption, text, TrayIcon.MessageType.NONE);
	}

	public void actionPerformed(ActionEvent e){
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void mouseClicked(MouseEvent e){
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void mousePressed(MouseEvent e){
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void mouseReleased(MouseEvent e){
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void mouseEntered(MouseEvent e){
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void mouseExited(MouseEvent e){
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void mouseDragged(MouseEvent e){
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void mouseMoved(MouseEvent e){
		//To change body of implemented methods use File | Settings | File Templates.
	}
}
